package org.jeecg.modules.basic.repair.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.aspect.annotation.Dict;
import org.jeecgframework.poi.excel.annotation.Excel;
import org.springframework.format.annotation.DateTimeFormat;

import java.io.Serializable;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.Date;

/**
 * @Description: 固定资产管理
 * @Author: jeecg-boot
 * @Date:   2023-02-07
 * @Version: V1.0
 */
@Data
@TableName("lwgy_fixed")
@Accessors(chain = true)
@EqualsAndHashCode(callSuper = false)
@ApiModel(value="lwgy_fixed对象", description="固定资产")
public class LwgyFixed implements Serializable {
    private static final long serialVersionUID = 1L;

	/**编号*/
	@TableId(type = IdType.ASSIGN_ID)
    @ApiModelProperty(value = "编号")
    private String  id;
    /**校区*/
    @Excel(name = "所属校区", width = 15,dictTable = "lwgy_base_apartment", dicText = "name", dicCode = "id")
    @Dict(dictTable = "lwgy_base_apartment", dicText = "name", dicCode = "id")
    @ApiModelProperty(value = "所属校区")
    private String campusId;


    @ApiModelProperty(value = "资产编号")
    private String assetsNo;
    @ApiModelProperty(value = "资产名称")
    private String assetsName;
    @ApiModelProperty(value = "物品id")
    private String model;
    @ApiModelProperty(value = "资产类别")
    private String assetsTypeName;
    @ApiModelProperty(value = "生产厂家")
    private String manuFacturer;
    /**创建日期*/
    @Excel(name = "置办日期", width = 15, format = "yyyy-MM-dd")
    @JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd")
    @DateTimeFormat(pattern="yyyy-MM-dd")
    @ApiModelProperty(value = "置办日期")
    private  Date manuDate;
    /**创建日期*/
    @JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd")
    @DateTimeFormat(pattern="yyyy-MM-dd")
    @ApiModelProperty(value = "置办日期")
    private Date buyDate;
    @ApiModelProperty(value = "所属部门")
    private String beDep;
    @ApiModelProperty(value = "保管人")
    private String custodian;
    @ApiModelProperty(value = "物品id")
    private String amouts;
    @ApiModelProperty(value = "残值率")
    private String remainValRate;
    @ApiModelProperty(value = "折旧类型")
    private String depreTypeId;
    @JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd")
    @DateTimeFormat(pattern="yyyy-MM-dd")
    @ApiModelProperty(value = "开始折旧日期")
    private Date startDepre;
    @ApiModelProperty(value = "总使用年限")
    private String intendTerm;
    @ApiModelProperty(value = "物品")
    private String intendWorkGross;
    @ApiModelProperty(value = "物品")
    private String workGrossUnit;
    @ApiModelProperty(value = "资产原值")
    private String assetValue;
    @ApiModelProperty(value = "资产当前值")
    private String assetCurValue;
    @ApiModelProperty(value = "开始日期")
    private String depreRate;
    @ApiModelProperty(value = "物品")
    private String defPerWorkGross;
    @ApiModelProperty(value = "物品")
    private String destroyDate;
    @ApiModelProperty(value = "物品")
    private String destroy;
    @ApiModelProperty(value = "报废去向")
    private String destroyWhere;
    @ApiModelProperty(value = "报废原因")
    private String destroyReason;
    @Excel(name = "折旧类型", width = 15,dicCode = "method_state")
    @ApiModelProperty(value = "折旧类型")
    @Dict(dicCode = "method_state")
    private String destroyId;
    @ApiModelProperty(value = "物品状态")
    @Dict(dicCode = "scrap_state")
    private String destoryStatus;

    @ApiModelProperty(value = "预计残值")
    private String salvageValue;

   /* @ApiModelProperty(value = "折旧额")
    private String amount;*/

    @ApiModelProperty(value = "工作量")
    private String workLoad;

    /**创建人*/
    @ApiModelProperty(value = "创建人")
    private String createBy;
    /**创建日期*/
    @JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd  HH:mm:ss")
    @DateTimeFormat(pattern="yyyy-MM-dd  HH:mm:ss")
    @ApiModelProperty(value = "创建日期")
    private Date createTime;
    /**更新人*/
    @ApiModelProperty(value = "更新人")
    private String updateBy;
    /**更新日期*/
    @JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd")
    @DateTimeFormat(pattern="yyyy-MM-dd")
    @ApiModelProperty(value = "更新日期")
    private Date updateTime;




    // 计算使用双倍余额递减法的折旧额和固定资产净值
  /*  public void calculateDepreciationByDoubleDecline() {
        double assetCost = Double.parseDouble(this.assetValue);  // 资产原值
        System.err.println("888:"+assetCost);
        double salvageValue = Double.parseDouble(this.salvageValue);  // 预计残值
        System.err.println("999:"+salvageValue);
        int intendTerm = Integer.parseInt(this.intendTerm);  // 总使用年限
        System.err.println("total:"+intendTerm);
        double depreciationBase = assetCost - salvageValue;  // 折旧基数
        System.err.println("depreciationBase:"+depreciationBase);
        double depreciationRate = (2.0 * depreciationBase) / intendTerm;  // 折旧率（双倍余额递减法）
        System.err.println("depreciationRate:"+depreciationRate);
        double netValue = assetCost;  // 固定资产净值
        System.err.println("netValue:"+netValue);
        double totalDepreciation = 0;  // 总折旧额
        for (int i = 1; i <= intendTerm; i++) {
            double depreciation = netValue * depreciationRate;  // 折旧额
            totalDepreciation += depreciation;  // 总折旧额累加
            netValue -= depreciation;  // 固定资产净值
        }
       *//* for (int i = 1; i <= intendTerm; i++) {
            double depreciation = netValue * depreciationRate;  // 折旧额
            netValue -= depreciation;  // 固定资产净值
        }*//*

       *//* DecimalFormat df = new DecimalFormat("0.00");  // 保留两位小数
        this.destroyId = "双倍余额递减法";  // 折旧类型为固定资产
        this.startDepre = new Date();  // 折旧日期为当前日期
        this.depreRate = df.format(depreciationRate * 100);  // 折旧率（百分比形式）
        this.amouts = df.format(depreciationBase - salvageValue - netValue);  // 折旧额
        this.assetValue = df.format(netValue);  // 固定资产净值
        this.assetCurValue = this.assetValue;  // 当前资产值等于固定资产净值*//*
        DecimalFormat df = new DecimalFormat("0.00");  // 保留两位小数
        this.destroyId = "双倍余额递减法";  // 折旧类型为固定资产
        this.startDepre = new Date();  // 折旧日期为当前日期
        this.depreRate = df.format(depreciationRate * 100);  // 折旧率（百分比形式）
        System.out.println("depreRate"+depreRate);
        this.amouts = df.format(totalDepreciation);  // 总折旧额
        System.out.println("amouts"+amouts);
        double currentAssetValue = assetCost - totalDepreciation - salvageValue;
        System.out.println("currentAssetValue"+currentAssetValue);
        this.assetValue = df.format(currentAssetValue);  // 当前资产值
        System.out.println("assetValue"+assetValue);
        this.assetCurValue = this.assetValue;  // 当前资产值等于固定资产净值
    }*/
    public void calculateDepreciationByDoubleDecline() {
        double assetCost = Double.parseDouble(this.assetValue);  // 资产原值
        System.err.println("888:" + assetCost);
        double salvageValue = Double.parseDouble(this.salvageValue);  // 预计残值
        System.err.println("999:" + salvageValue);
        int intendTerm = Integer.parseInt(this.intendTerm);  // 总使用年限
        System.err.println("total:" + intendTerm);
        double depreciationBase = assetCost - salvageValue;  // 折旧基数
        System.err.println("depreciationBase:" + depreciationBase);
        double depreciationRate = (2.0 * depreciationBase) / intendTerm;  // 折旧率（双倍余额递减法）
        System.err.println("depreciationRate:" + depreciationRate);
        double netValue = assetCost;  // 固定资产净值
        System.err.println("netValue:" + netValue);
        double totalDepreciation = 0;  // 总折旧额
        for (int i = 1; i <= intendTerm; i++) {
            double depreciation = netValue * depreciationRate;  // 折旧额
            totalDepreciation += depreciation;  // 总折旧额累加
            netValue -= depreciation;  // 固定资产净值
        }
        DecimalFormat df = new DecimalFormat("0.00");  // 保留两位小数
        this.destroyId = "1";  // 折旧类型为固定资产
        this.startDepre = new Date();  // 折旧日期为当前日期
        this.depreRate = df.format(depreciationRate * 100);  // 折旧率（百分比形式）
        this.amouts = df.format(totalDepreciation);  // 总折旧额
        System.out.println("amouts"+amouts);
        double currentAssetValue = assetCost - totalDepreciation - salvageValue;
        System.out.println("currentAssetValue"+currentAssetValue);
        this.assetValue = df.format(currentAssetValue);  // 当前资产值
        System.out.println("assetValue"+assetValue);
        this.assetCurValue = this.assetValue;  // 当前资产值等于固定资产净值
    }

    // 计算使用工作量法的折旧额和固定资产净值
    public void calculateDepreciationByWorkload() {
        double assetCost = Double.parseDouble(this.assetValue);  // 资产原值
        double salvageValue = Double.parseDouble(this.salvageValue);  // 预计残值
        double workload = Double.parseDouble(this.workLoad);  // 工作量
        double depreciationBase = assetCost - salvageValue;  // 折旧基数
        double depreciationRate = depreciationBase / workload;  // 折旧率（工作量法）
        double netValue = assetCost;  // 固定资产净值

        double depreciation = workload * depreciationRate;  // 折旧额
        netValue -= depreciation;  // 固定资产净值

        DecimalFormat df = new DecimalFormat("0.00");  // 保留两位小数
        this.destroyId = "2";  // 折旧类型为无形资产
        this.startDepre = new Date();  // 折旧日期为当前日期
        this.depreRate = df.format(depreciationRate * 100);  // 折旧率（百分比形式）
        this.amouts = df.format(depreciation);  // 折旧额
        this.assetValue = df.format(netValue);  // 固定资产净值
        this.assetCurValue = this.assetValue;  // 当前资产值等于固定资产净值
    }

    // 计算使用平均年限法的折旧额和固定资产净值
    public void calculateDepreciationByAverageYear() {
        double assetCost = Double.parseDouble(this.assetValue);  // 资产原值
        double salvageValue = Double.parseDouble(this.salvageValue);  // 预计残值
        int totalYear = Integer.parseInt(this.intendTerm);  // 总使用年限
        double depreciationBase = assetCost - salvageValue;  // 折旧基数
        double depreciationRate = depreciationBase / (totalYear * (totalYear + 1) / 2);  // 折旧率（平均年限法）
        double netValue = assetCost;  // 固定资产净值

        for (int i = 1; i <= totalYear; i++) {
            double depreciation = depreciationRate * (totalYear - i + 1);  // 折旧额
            netValue -= depreciation;  // 固定资产净值
        }

        DecimalFormat df = new DecimalFormat("0.00");  // 保留两位小数
        this.destroyId = "3";  // 折旧类型为无形资产
        this.startDepre = new Date();  // 折旧日期为当前日期
        this.depreRate = df.format(depreciationRate * 100);  // 折旧率（百分比形式）
        this.amouts = df.format(depreciationBase - salvageValue - netValue);  // 折旧额
        this.assetValue = df.format(netValue);  // 固定资产净值
        this.assetCurValue = this.assetValue;  // 当前资产值等于固定资产净值
    }

    // 计算使用年度总和法的折旧额和固定资产净值
    public void calculateDepreciationBySumOfYearsDigits() {
        double assetCost = Double.parseDouble(this.assetValue);  // 资产原值
        double salvageValue = Double.parseDouble(this.salvageValue);  // 预计残值
        int totalYear = Integer.parseInt(this.intendTerm);  // 总使用年限
        double depreciationBase = assetCost - salvageValue;  // 折旧基数
        double sumOfYearsDigits = totalYear * (totalYear + 1) / 2;  // 年数总和
        double netValue = assetCost;  // 固定资产净值

        for (int i = 1; i <= totalYear; i++) {
            double depreciationRate = (totalYear - i + 1) / sumOfYearsDigits;  // 折旧率（年度总和法）
            double depreciation = netValue * depreciationRate;  // 折旧额
            netValue -= depreciation;  // 固定资产净值
        }

        DecimalFormat df = new DecimalFormat("0.00");  // 保留两位小数
        this.destroyId = "4";  // 折旧类型为无形资产
        this.startDepre = new Date();  // 折旧日期为当前日期
        this.depreRate = "";  // 年度总和法没有固定的折旧率，设置为空字符串
        this.amouts= df.format(depreciationBase - salvageValue - netValue);  // 折旧额
        this.assetValue = df.format(netValue);  // 固定资产净值
        this.assetCurValue = this.assetValue;  // 当前资产值等于固定资产净值
    }





}
